package com.car.action.admin;

import java.util.List;

import com.car.common.model.CardDiscuss;
import com.car.common.model.ReportCards;
import com.car.common.model.ReportUsers;
import com.car.common.model.Tags;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.utils.StringUtil;
import com.utils.SystemClock;

public class TagsController extends Controller{

	public void index() {
		List<Tags> tagsList = Tags.dao.find("select id,name,parent_id,status,sort,matching,matching_name from tags where parent_id != 0 order by parent_id asc ,sort desc");
		setAttr("list", tagsList);
		render("index.html");
	}
	
	public void detail() {
		Tags one = Tags.dao.findById(getPara("id"));
		setAttr("one", one == null ? new Tags() : one);
		render("detail.html");
	}
	
	public void del() {
		Db.delete("delete from tags where id = " + getPara("id"));
		renderText("操作成功!");
	}
	
	public void save() {
		Tags one = Tags.dao.findById(getPara("id"));
		boolean updateFlg = false;
		if (one == null) {
			 one = new Tags();
			 one.setCreateTime(SystemClock.now()/1000L);
			 updateFlg = true;
		}
		one.setParentId(getParaToInt("parent_id"));
		one.setName(getPara("name"));
		one.setSort(getParaToInt("sort"));
		one.set("matching", getPara("matching"));
		String matching = getPara("matching");
		if (!StringUtil.empty(matching)) {
			String matching_name = "";
			String[] nameArray = matching.split(",");
			for(String name:nameArray){
				Tags one1 = Tags.dao.findById(name);
				if(one1 != null) {
					matching_name +=one1.getName() +  ",";
				}
			}
			one.set("matching_name", matching_name);
		}
		if (updateFlg) {
			one.save();
		} else {
			one.update();
		}
		renderText("操作成功!");
	}
	
	
	public void report_users() {
		int page = 1;
        if(!StringUtil.empty(getPara("page"))) {
        	page = getParaToInt("page");
        } 
        String sql = "select r.*,u.user_mobile,u.nick_name,CASE r.status WHEN '2' THEN '已处理'  ELSE '已处理' END as status_name , FROM_UNIXTIME(r.create_time,'%Y-%m-%d %H:%i') as create_time_string";
        String andSql = " from report_users as r left join users as u on u.id = r.report_user_id order by r.status  desc,r.create_time asc ";
        Page<ReportUsers> pageData = ReportUsers.dao.paginate(page, 15, sql,andSql);
        setAttr("page", pageData);
        render("report_users.html");
	}
	
	public void report_cards() {
		int page = 1;
        if(!StringUtil.empty(getPara("page"))) {
        	page = getParaToInt("page");
        } 
        String sql = "select r.*, FROM_UNIXTIME(r.create_time,'%Y-%m-%d %H:%i') as create_time_string";
        String andSql = " from report_cards as r order by r.status  desc,r.create_time asc ";
        Page<ReportCards> pageData = ReportCards.dao.paginate(page, 15, sql,andSql);
        setAttr("page", pageData);
        render("report_cards.html");
	}
}
